﻿using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Data.Sqlite;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using KorbeRTR.Utility;

namespace KorbeRTR.Models
{
    /// <summary>
    /// 下料记录
    /// </summary>
    [Table(Name = "Loadings")]
    public class LoadingView
    {
        /// <summary>
        /// 下料记录ID
        /// </summary>
        [Column]
        public Int64 Id { get; set; }

        /// <summary>
        /// 软删除标志
        /// </summary>
        [Column]
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 下料时间
        /// </summary>
        [Column]
        public DateTime Time { get; set; }

        /// <summary>
        /// 下料员
        /// </summary>
        [Column]
        public string Loader { get; set; }

        /// <summary>
        /// 批号
        /// </summary>
        [Column]
        public string Batch { get; set; }

        /// <summary>
        /// 工单ID
        /// </summary>
        [Column(Name = "GDID")]
        public string 工单ID { get; set; }

        /// <summary>
        /// 物料编码
        /// </summary>
        [Column(Name = "WLBM")]
        public string 物料编码 { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        [Column]
        public Int64 Count { get; set; }

        /// <summary>
        /// 料号ID
        /// </summary>
        [Column]
        public Int64 LoadingRecipeId { get; set; }

        /// <summary>
        /// mm
        /// </summary>
        [Column(Name = "GCZXZKKJ")]
        public double 过程最小钻孔孔径 { get; set; }

        /// <summary>
        /// mm
        /// </summary>
        [Column(Name = "KHYQJGKZXKJ")]
        public double 客户要求激光孔最小孔径 { get; set; }

        /// <summary>
        /// 料号内容
        /// </summary>
        public RecipeView LoadingRecipe { get; set; }

        public LoadingView()
        {
            LoadingRecipe = new RecipeView();
        }

        /// <summary>
        /// 添加下料记录
        /// </summary>
        /// <returns></returns>
        public static bool Add(LoadingView view)
        {
            var id = RecipeView.Add("LoadingRecipes", "LoadingCuSlotRecipes", view.LoadingRecipe);

            var row = RecipesDBHelper.ExecuteNonQuery(
                @"INSERT INTO Loadings(Time,Loader,Batch,GDID,WLBM,Count,LoadingRecipeId,GCZXZKKJ,KHYQJGKZXKJ) VALUES(@Time,@Loader,@Batch,@GDID,@WLBM,@Count,@LoadingRecipeId,@GCZXZKKJ,@KHYQJGKZXKJ)",
                new SqliteParameter("@Time", view.Time.ToSqlTime()),
                new SqliteParameter("@Loader", view.Loader),
                new SqliteParameter("@Batch", view.Batch),
                new SqliteParameter("@GDID", view.工单ID ?? ""),
                new SqliteParameter("@WLBM", view.物料编码 ?? ""),
                new SqliteParameter("@Count", view.Count),
                new SqliteParameter("@LoadingRecipeId", id),
                new SqliteParameter("@GCZXZKKJ", -1),
                new SqliteParameter("@KHYQJGKZXKJ", -1)
                );

            return row > 0;
        }

        /// <summary>
        /// 列出全部下料记录
        /// </summary>
        public static List<LoadingView> ListAll()
        {
            var list = new List<LoadingView>();
            {   // 读取下料记录
                var dt = RecipesDBHelper.ExecuteTable("SELECT * FROM Loadings WHERE IsDeleted=FALSE ORDER BY Time DESC");
                list = dt.ToModel<LoadingView>();
            }

            {   // 读取镀铜槽参数
                var dict = RecipeView.ListAll("LoadingRecipes", "LoadingCuSlotRecipes");

                // 配方参数和镀铜槽参数匹配
                foreach (var loading in list)
                {
                    if (dict.ContainsKey(loading.LoadingRecipeId))
                    {
                        loading.LoadingRecipe = dict[loading.LoadingRecipeId];
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 查询 t0 到 t1 时间内的记录
        /// </summary>
        public static List<LoadingView> Select(DateTime t0, DateTime t1)
        {
            if (t0 > t1)
            {
                AuxLib.Swap(ref t0, ref t1);
            }

            var list = new List<LoadingView>();
            {
                var dt = RecipesDBHelper.ExecuteTable("SELECT * FROM Loadings WHERE IsDeleted=FALSE AND (Time BETWEEN @T0 AND @T1) ORDER BY Time DESC",
                    new SqliteParameter("@T0", t0.ToSqlTime()),
                    new SqliteParameter("@T1", t1.ToSqlTime())
                    );

                list = dt.ToModel<LoadingView>();
            }

            {   // 读取镀铜槽参数
                var dict = RecipeView.ListAll("LoadingRecipes", "LoadingCuSlotRecipes");

                // 配方参数和镀铜槽参数匹配
                foreach (var loading in list)
                {
                    if (dict.ContainsKey(loading.LoadingRecipeId))
                    {
                        loading.LoadingRecipe = dict[loading.LoadingRecipeId];
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 查询N天数据
        /// </summary>
        /// <param name="start">起始日期</param>
        /// <param name="days">天数</param>
        /// <returns></returns>
        public static List<LoadingView> Select(DateTime aday, int days)
        {
            var t0 = aday.Date;
            var t1 = aday.Date.AddDays(days);
            return Select(t0, t1);
        }

        /// <summary>
        /// 查询某天的数据
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        public static List<LoadingView> Select(DateTime day)
        {
            return Select(day, 1);
        }
    }
}
